fix: guard token-count divisions against ZeroDivisionError (#183)#245
Open
ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: guard token-count divisions against ZeroDivisionError (#183)#245ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…#183) compress_prompt_llmlingua2 and structured_compress_prompt computed target_token / n_original_token (and its siblings) in four spots. Under certain structured-compress payloads — e.g. a compress_json call where every key is force-reserved or the context resolves to empty strings — the denominator collapses to zero and the call dies with "ZeroDivisionError: float division by zero" before the user ever sees a compressed result. Wrap each division in an `if n > 0` guard that falls back to rate=1.0 (i.e. keep everything) when no source tokens are available. Covered sites: structured_compress_prompt (context_tokens_length), compress_prompt_llmlingua2 (n_original_token at the context-level and token-level paths, n_reserved_token at the context-level path). Generated by Claude Code Vibe coded by ousamabenyounes Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #183
compress_prompt_llmlingua2andstructured_compress_promptcomputetarget_token / n_original_token(and its siblings) in four places. Under certain structured-compress payloads — e.g. acompress_jsoncall where every key is force-reserved, or the context after tag stripping resolves to empty strings — the denominator collapses to zero and the call dies withZeroDivisionError: float division by zerobefore the user ever sees a compressed result. The reporter hit this on the exact repro in the issue.Fix
Wrap each division in an
if n > 0guard that falls back torate = 1.0(keep everything) when there are no source tokens to compress. The fallback preserves the semantic meaning of "there is nothing to compress here" without swallowing the error silently — downstream code handles `rate >= 1.0` correctly.Sites patched
The two other divisions in the same function (`n_original_token / n_compressed_token` for `ratio`) already had the `1 if n_compressed_token == 0 else …` guard — left untouched.
Before submitting
use_llmlingua2#183.Verification
Generated by Claude Code
Vibe coded by ousamabenyounes